﻿Imports System.IO
Imports System.Data
Imports MetodosComuns
Imports extranet.BC
Imports extranet.BE
Imports System.Collections.Generic

Partial Class Perfil
  Inherits System.Web.UI.Page

  Private Const MESSAGE_ERRO_NA_INSERCAO As String = "Erro na inserção do Registro!."
  Private Const MESSAGE_ERRO_NA_ALTERACAO As String = "Erro na atualização do Registro!."
  Private Const MESSAGE_ERRO_NA_EXCLUSAO As String = "Erro na exclusão do Registro!."

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '==================       PROCEDIMENTO  PADRÃO  SEGURANÇA     =============================
    Dim objBESeguranca As BESeguranca
    Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
    If IsNothing(objBEUsuario) Then Response.Redirect(TELA_LOGOUT)
    Session("strTela") = MetodosComuns.GetCurrentPageName()
    If Not IsPostBack Then          ' SEGURANÇA
      objBESeguranca = BCSeguranca.SelecionaPermissoes(objBEUsuario.GrupoAcesso, Session("strTela"))
      If Not objBESeguranca.Autorizado Then Response.Redirect(TELA_LOGOUT)
      Session("objBESeguranca") = objBESeguranca
    Else
      objBESeguranca = Session("objBESeguranca")
    End If

    '==================       ALTERAR SE FOR NECESSARIO          ===============================
    If Not IsPostBack Then
      mostraRegistro()
      mostraRecados()
    End If
  End Sub

  Protected Sub mostraRegistro()
    Dim listUsuario As List(Of BEUsuario) = BCUsuario.Selecionar(Session("UsuarioParaConsulta"))
    If listUsuario.Count > 0 Then lblTituloPagina.Text = listUsuario(0).Nome
    fv_Usuario.DataSource = listUsuario
    fv_Usuario.DataBind()
  End Sub

  Protected Sub mostraRecados()
    Dim listRecados As List(Of BERecado) = BCRecado.Selecionar(Session("UsuarioParaConsulta"))
    With DirectCast(fv_Usuario.FindControl("grvRecados"), GridView)
      .DataSource = listRecados
      .DataBind()
    End With
  End Sub

  Protected Sub lbl_Franquia_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Session("FranquiaParaConsulta") = DirectCast(BCUsuario.Selecionar(Session("UsuarioParaConsulta"))(0), BEUsuario).Franquia
    Response.Redirect("Franquia.aspx")
  End Sub

  Protected Sub grvRecados_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
    Try
      Dim objGridView As GridView = sender
      BCRecado.Excluir(New BERecado(Integer.Parse(objGridView.DataKeys(e.RowIndex)("IdRecado"))))
      grvRecados_RowDeleted(sender, Nothing)
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Protected Sub grvRecados_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs)
    If e IsNot Nothing Then e.ExceptionHandled = True
    mostraRecados()
  End Sub

  Protected Sub grvRecados_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs)
    DirectCast(sender, GridView).PageIndex = e.NewPageIndex
    mostraRecados()
  End Sub

  Protected Sub btnEnviarRecado_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim txtRecado As TextBox = fv_Usuario.FindControl("txt_recado")
    Dim objRemetente As BEUsuario = Session("objBEUsuario")
    BCRecado.Cadastrar(New BERecado(0, objRemetente.Usuario, Session("UsuarioParaConsulta"), txtRecado.Text, Nothing))
    mostraRecados()
    txtRecado.Text = ""
  End Sub

  Protected Sub grvRecados_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
    For Each objRow As GridViewRow In DirectCast(sender, GridView).Rows()
      If Not (DirectCast(objRow.FindControl("hdn_Remetente"), HiddenField).Value = objBEUsuario.Usuario OrElse _
         objBEUsuario.GrupoAcesso.Nome.ToUpper() = "ADMIN") Then
        With DirectCast(objRow.FindControl("btnExcluir"), ImageButton)
          .CommandName = ""
          .ImageUrl = "~/Imagens/cleardot.gif"
        End With
      End If
    Next
  End Sub

End Class
